Page({
  data: {
    currentExhibit: null,
    isScanning: false,
    exhibits: [
      {
        id: 1,
        name: "青铜器",
        image: "/images/exhibit1.jpg",
        description: "战国时期青铜器，具有极高的历史价值和艺术价值。这件青铜器采用失蜡法铸造，器身纹饰精美...",
        audioUrl: "cloud://xxx.mp3"
      },
      {
        id: 2,
        name: "汉代陶俑",
        image: "/images/exhibit2.jpg",
        description: "汉代陶俑，反映了当时的社会生活状态。陶俑造型生动，体现了汉代制陶工艺的高超水平...",
        audioUrl: "cloud://xxx.mp3"
      },
      {
        id: 3,
        name: "宋代瓷器",
        image: "/images/exhibit3.jpg",
        description: "宋代青瓷，釉色莹润，胎体细腻。这件瓷器采用了独特的刻花工艺...",
        audioUrl: "cloud://xxx.mp3"
      }
    ],
    audioContext: null,
    isPlaying: false
  },

  onLoad: function() {
    // 初始化相机权限
    wx.authorize({
      scope: 'scope.camera',
      success: () => {
        console.log('相机授权成功')
      },
      fail: () => {
        wx.showToast({
          title: '需要相机权限才能识别展品',
          icon: 'none'
        })
      }
    })

    // 初始化音频播放器
    this.initAudioContext()
  },

  // 初始化音频上下文
  initAudioContext: function() {
    this.audioContext = wx.createInnerAudioContext()
    this.audioContext.onPlay(() => {
      console.log('开始播放讲解')
      this.setData({ isPlaying: true })
    })
    this.audioContext.onEnded(() => {
      console.log('讲解结束')
      this.setData({ isPlaying: false })
    })
    this.audioContext.onError((err) => {
      console.error('音频播放错误', err)
      wx.showToast({
        title: '音频播放失败',
        icon: 'none'
      })
      this.setData({ isPlaying: false })
    })
  },

  // 选择展品
  selectExhibit: function(e) {
    const id = e.currentTarget.dataset.id
    const exhibit = this.data.exhibits.find(item => item.id === id)
    
    // 记录用户选择的展品
    console.log('用户选择展品:', {
      id: exhibit.id,
      name: exhibit.name,
      time: new Date().toLocaleString()
    })

    this.setData({
      currentExhibit: exhibit,
      isPlaying: false
    })

    // 停止之前的音频
    if (this.audioContext) {
      this.audioContext.stop()
    }
  },

  // 开始扫描识别
  startScan: function() {
    this.setData({ isScanning: true })
    
    // 模拟展品识别过程
    wx.showLoading({
      title: '正在识别展品...'
    })

    // 模拟识别延迟
    setTimeout(() => {
      wx.hideLoading()
      
      // 随机选择一个展品作为识别结果
      const randomIndex = Math.floor(Math.random() * this.data.exhibits.length)
      const recognizedExhibit = this.data.exhibits[randomIndex]
      
      this.setData({
        currentExhibit: recognizedExhibit,
        isScanning: false
      })

      wx.showToast({
        title: '识别成功',
        icon: 'success'
      })
    }, 2000)
  },

  // 播放讲解
  playAudio: function() {
    if (!this.data.currentExhibit) {
      wx.showToast({
        title: '请先选择展品',
        icon: 'none'
      })
      return
    }

    // 记录用户请求讲解
    console.log('用户请求讲解:', {
      exhibitId: this.data.currentExhibit.id,
      exhibitName: this.data.currentExhibit.name,
      time: new Date().toLocaleString()
    })

    if (this.data.isPlaying) {
      // 如果正在播放，则停止
      this.audioContext.stop()
      this.setData({ isPlaying: false })
    } else {
      // 开始播放新的讲解
      this.audioContext.src = this.data.currentExhibit.audioUrl
      this.audioContext.play()
      
      wx.showToast({
        title: '正在讲解: ' + this.data.currentExhibit.name,
        icon: 'none',
        duration: 2000
      })
    }
  },

  onUnload: function() {
    // 页面卸载时释放音频资源
    if (this.audioContext) {
      this.audioContext.destroy()
    }
  }
}) 